package com.hiof.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.hiof.utility.Utility;

/**
 * Created by IntelliJ IDEA.
 * User: manish
 * Date: 4/5/12
 * Time: 11:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class MoraSqliteHelper extends SQLiteOpenHelper {

    public static final String TABLE_LATEST_USERINFO = "tbl_latest_userinfo";
    public static final String TABLE_NEXT_TARGET = "tbl_next_target";
    public static final String USER_ID = "userid";
    public static final String LAT = "latitude";
    public static final String LON = "longitude";
    public static final String HEIGHT = "altitude";
    public static final String SPEED = "speed";
    public static final String TYPE = "user_type";
    public static final String TIME = "time";
    public static final String SELF = "self";
    public static final String DISTANCE = "distance";
    public static final String TARGET_NAME = "target_name";
    public static final String PREDICTED_TIME = "predicted_time";



    private static final String DATABASE_NAME = "moraa.db";
    private static final int DATABASE_VERSION = 6;

    // Database creation sql statement
    private static final String CREATE_LATEST_USERINFO_TABLE = "create table "
            + TABLE_LATEST_USERINFO
            + "( "
            + USER_ID + " INTEGER, "
            + LAT + " REAL,"
            + LON + " REAL,"
            + HEIGHT + " REAL,"
            + SPEED + " REAL,"
            + TIME + " time DATE,"
            + TYPE + " String,"
            + SELF + " String"
            + ");";
    private static final String CREATE_NEXT_TARGET_TABLE = "create table "
            + TABLE_NEXT_TARGET
            + "( "
            + DISTANCE + " REAL, "
            + LAT + " REAL,"
            + LON + " REAL,"
            + HEIGHT + " REAL,"
            + PREDICTED_TIME + " REAL,"
            + TARGET_NAME + " String"
            + ");";

    public MoraSqliteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        Utility.debug(this.getClass().toString(), "New tables created");
        sqLiteDatabase.execSQL(CREATE_LATEST_USERINFO_TABLE);
        sqLiteDatabase.execSQL(CREATE_NEXT_TARGET_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_LATEST_USERINFO);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NEXT_TARGET);
        onCreate(sqLiteDatabase);
    }
}
